#!/bin/bash
#controller1节点
sed -i 's/bind-address = 0.0.0.0/bind-address = controller1/' /etc/my.cnf.d/openstack.cnf
echo '
wsrep_cluster_address="gcomm://controller1,controller2,controller3"
wsrep_node_name=controller1
wsrep_node_address=controller1
'>>/etc/my.cnf.d/galera.cnf
cat /etc/my.cnf.d/galera.cnf

systemctl daemon-reload
systemctl stop mariadb.service
#启动第一个节点
galera_new_cluster 

read -p "将controller2、controller3加入集群，待其它节点重新启动后，重启第一个节点:" 

systemctl restart mariadb.service

#SQL root密码
DBPass=innobase
#检测
netstat -antp|grep mysqld
mysql -u root -p$DBPass -e "show status like 'wsrep_cluster_size';"
mysql -u root -p$DBPass -e "show status like 'wsrep_incoming_addresses';"

#创建用于监控的mariadb 用户haproxy (haproxy代理，监控使用)
mysql -u root -p$DBPass -e "create user 'haproxy'@'%';flush privileges;"

#root远程访问设置
mysql -u root -p$DBPass -e "
grant all privileges on *.* to 'root'@'%' identified by '$DBPass' with grant option; 
flush privileges;
select user,host,password from mysql.user;"

##########################################

##部署成功，检测结果如下      
# [root@controller1 ~]# mysql -u root -p$DBPass -e "show status like 'wsrep_cluster_size';"
# +--------------------+-------+
# | Variable_name      | Value |
# +--------------------+-------+
# | wsrep_cluster_size | 3     |
# +--------------------+-------+
# [root@controller1 ~]# mysql -u root -p$DBPass -e "show status like 'wsrep_incoming_addresses';"
# +--------------------------+----------------------------------------------------+
# | Variable_name            | Value                                              |
# +--------------------------+----------------------------------------------------+
# | wsrep_incoming_addresses | controller1:3306,controller2:3306,controller3:3306 |
# +--------------------------+----------------------------------------------------+



# #排错
# #关闭selinux、防火墙
# tail /var/log/mariadb/mariadb.log
# rm -f /var/lib/mysql//galera.cache
# #关闭群集后，重启
# #在其中一台
# sed -i 's#seqno: -1#seqno: 1#' /var/lib/mysql/grastate.dat
# galera_new_cluster #启动第一个节点
# #其它节点
# systemctl start mariadb


#test
#/usr/libexec/mysqld --wsrep-new-cluster --user=root &
#systemctl start mariadb.service
#设置开机启动，设置为第一个节点
# systemctl disable mariadb.service
# echo '#作为第一个节点启动SQL
# /usr/bin/galera_new_cluster
# sleep 5
# ssh controller2 "systemctl start mariadb.service"
# ssh controller3 "systemctl start mariadb.service"
# sleep 3
# systemctl restart mariadb.service
# '>>/etc/rc.local
